﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestData;

namespace TestData.Builders
{
    public class PersonBuilder
    {
        private int? num;

        private Gender? gender;
        private AgeRange? ageRange;

        public PersonBuilder WithGender(Gender g)
        {
            num = null;
            gender = g;
            return this;
        }

        public PersonBuilder WithAgeRange(AgeRange a)
        {
            ageRange = a;
            return this;
        }

        public RandomPerson Build()
        {
            RandomData.CheckInit();
            RandomPerson toReturn = new RandomPerson ();
            int num = 0;
            int lastNum = 0;

            if (gender != null)
                toReturn.Gender = gender.Value;
            else
                toReturn.Gender = RandomData.GetRandomGender();

            if (ageRange != null)
                toReturn.AgeRange = ageRange.Value;
            else
                toReturn.AgeRange = RandomData.GetRandomAgeRange();

            List<string> nameList;
            if (toReturn.Gender == Gender.Male)
                nameList = RandomData.firstMale;
            else
                nameList = RandomData.firstFemale;

            num = RND.GetRandomInt(0, nameList.Count -1);
            toReturn.FirstName = nameList[num]; 

            lastNum = num;
            while (num == lastNum)
            {
                num = RND.GetRandomInt(0, nameList.Count -1);
            }

            toReturn.MiddleName = nameList[num];

            num = RND.GetRandomInt(0, RandomData.lastNames.Count -1);
            toReturn.LastName = RandomData.lastNames[num];

            toReturn.Address = AddressBuilder.Build();
            int minYear = 0;
            int maxYear = 0;
            if (toReturn.AgeRange  == AgeRange.Adult)
            {
                minYear = DateTime.Now.Year  - 60;
                maxYear = DateTime.Now.Year  - 18;
            }
            else
            {
                minYear = DateTime.Now.Year - 18;
                maxYear = DateTime.Now.Year;
            }

            toReturn.BirthDay = RND.getRandomDate(
                new DateTime(minYear, 1, 1), new DateTime(maxYear, 1, 1));

            toReturn.SSN = RND.GetRandomNumericString (9);

            return toReturn;
            
        }

    }
}
